package com.bestcem.xm.award.controller.convert;

import cn.hutool.core.collection.CollectionUtil;
import com.bestcem.bp.grpc.v1.base.FileInfo;
import com.bestcem.xm.award.controller.dto.AwardWinDTO;
import com.bestcem.xm.award.controller.dto.WinDataDTO;
import com.bestcem.xm.award.enums.WinCodeStatusEnum;
import com.bestcem.xm.award.grpc.v1.services.AwardWin;
import com.bestcem.xm.award.grpc.v1.services.WinData;
import com.bestcem.xm.award.service.bo.award.AwardWinBO;
import com.bestcem.xm.award.service.bo.award.FileInfoBO;
import com.bestcem.xm.award.service.bo.award.WinDataBO;
import com.bestcem.xm.award.service.dto.FileInfoDTO;
import com.bestcem.xm.common.core.uitls.EnumUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 兑奖码数据转换
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v3.0
 * @date 2021/8/18 18:08
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class WinDataConvert {

    /**
     * 兑奖码参数转换，目前只有传输list一种方式
     * @param winData
     * @return WinDataDTO
     */
    public static WinDataDTO winDataToWinDataDTO(WinDataBO winData) {
        if (winData == null || CollectionUtil.isEmpty(winData.getWins())) {
            return null;
        }
        WinDataDTO winDataDTO = new WinDataDTO();
        winDataDTO.setStatus(WinCodeStatusEnum.SMALL_WIN.getIndex());
        List<AwardWinDTO> list = new ArrayList<>();
        winData.getWins().stream().forEach(win -> {
            AwardWinDTO awardWinDTO = new AwardWinDTO();
            awardWinDTO.setCode(win.getCode());
            awardWinDTO.setLevel(win.getLevel());
            awardWinDTO.setName(win.getName());
            list.add(awardWinDTO);
        });
        winDataDTO.setWins(list);
        return winDataDTO;
    }

    /**
     * 参数转换
     * @param winData
     * @return
     */
    public static WinDataBO winDataToWinDataBO(WinData winData) {
        if (winData == null) {
            return null;
        }
        WinDataBO winDataBO = new WinDataBO();
        winDataBO.setStatus(EnumUtil.getEnumByIndex(WinCodeStatusEnum.class, winData.getStatusValue()).getIndex());
        winDataBO.setTempId(winData.getTempId());
        if (winData.getWinsList() != null && winData.getWinsList().size() > 0) {
            List<AwardWinBO> winBOList = winData.getWinsList().stream().map(win -> {
                AwardWinBO awardWinBO = new AwardWinBO();
                awardWinBO.setCode(win.getCode()).setLevel(win.getLevelValue()).setName(win.getName());
                return awardWinBO;
            }).collect(Collectors.toList());
            winDataBO.setWins(winBOList);
        }
        if (winData.getFile() != null) {
            FileInfo file = winData.getFile();
            FileInfoBO fileInfoBO = new FileInfoBO();
            fileInfoBO.setId(file.getId()).setName(file.getName()).setType(file.getType()).setUrl(file.getUrl());
            winDataBO.setFile(fileInfoBO);
        }

        return winDataBO;
    }

    /**
     * 参数转换
     * @param winDataBO
     * @return
     */
    public static WinData winDataBOToWinData(WinDataBO winDataBO) {
        if (winDataBO == null) {
            return null;
        }
        WinData.Builder builder = WinData.newBuilder();
        builder.setStatusValue(winDataBO.getStatus());
        if (winDataBO.getTempId() != null) {
            builder.setTempId(winDataBO.getTempId());
        }
        if (winDataBO.getWins() != null && winDataBO.getWins().size() > 0) {
            winDataBO.getWins().stream().forEach(awardWinBO -> {
                AwardWin.Builder winBuilder = AwardWin.newBuilder();
                if (awardWinBO.getCode() != null) {
                    winBuilder.setCode(awardWinBO.getCode());
                }
                if (awardWinBO.getName() != null) {
                    winBuilder.setName(awardWinBO.getName());
                }
                if (awardWinBO.getLevel() != null) {
                    winBuilder.setLevelValue(awardWinBO.getLevel());
                }
                builder.addWins(winBuilder.build());
            });
        }
        if (winDataBO.getFile() != null) {
            FileInfoBO file = winDataBO.getFile();
            FileInfo.Builder fileBuilder = FileInfo.newBuilder();
            if (file.getUrl() != null) {
                fileBuilder.setUrl(file.getUrl());
            }
            if (file.getId() != null) {
                fileBuilder.setId(file.getId());
            }
            if (file.getName() != null) {
                fileBuilder.setName(file.getName());
            }
            if (file.getType() != null) {
                fileBuilder.setType(file.getType());
            }
            builder.setFile(fileBuilder.build());
        }
        return builder.build();
    }

    /**
     * 兑奖码参数转换 ，目前只有传输list一种方式
     * @param winDataDTO
     * @return WinData
     */
    public static WinDataBO windataDTOToWinDataBO(WinDataDTO winDataDTO) {
        if (winDataDTO == null || CollectionUtils.isEmpty(winDataDTO.getWins())) {
            return null;
        }
        WinDataBO winDataBO = new WinDataBO();
        winDataBO.setStatus(WinCodeStatusEnum.SMALL_WIN.getIndex());
        winDataBO.setTempId(winDataDTO.getTempId());
        if (winDataDTO.getWins() != null && winDataDTO.getWins().size() > 0) {
            List<AwardWinBO> winBOList = winDataDTO.getWins().stream().map(win -> {
                AwardWinBO awardWinBO = new AwardWinBO();
                awardWinBO.setCode(win.getCode()).setLevel(win.getLevel()).setName(win.getName());
                return awardWinBO;
            }).collect(Collectors.toList());
            winDataBO.setWins(winBOList);
        }
        if (winDataDTO.getFile() != null) {
            FileInfoDTO file = winDataDTO.getFile();
            FileInfoBO fileInfoBO = new FileInfoBO();
            fileInfoBO.setId(file.getId()).setName(file.getName()).setUrl(file.getUrl());
            winDataBO.setFile(fileInfoBO);
        }

        return winDataBO;
    }
}
